<?php
namespace app\alipay\controller;

use tp5_lib\account\logic\AccountLogic;
use tp5_lib\notify\extension\NotifyBaseController;
use tp5_lib\alipay\utility\Alipay;

/**
 * @todo 将支付宝的回调请求迁移到这个控制器中，并且完善`NotifyBaseController`
 */
class NotifyController extends NotifyBaseController
{
    protected $name="alipay";
    public function index()
    {
        $sign_type = empty($_POST["sign_type"]) ? "RSA" : $_POST["sign_type"];
        $alipay=new Alipay();
        if(!$alipay->client->rsaCheckV1($_POST, NULL, $sign_type)){
            $this->error("验签失败");
        }

        if($_POST["notify_type"] === "trade_status_sync"){
            if($_POST["trade_status"] === "TRADE_SUCCESS"){
                $account=new AccountLogic();
                if(!$account->success($_POST["out_trade_no"])){
                    $this->error("Account:".$account->getError());
                }
                $this->message="TRADE_SUCCESS";
            }
        }
        echo "success";exit;
    }
}
